﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using CRMFramework;
using CRMEntity;
using System.Net;

namespace CRMBiz
{
    public static class CmsMemberGroupHandler
    {
        public static List<CmsMemberGroup> GetAll()
        {
            return GetBy(null);
        }

        public static List<CmsMemberGroup> GetBy(int? userId)
        {
            try
            {
                var sbSql = new StringBuilder("SELECT mg.id NodeId, mg.text MemberGroupName, mg.CreateDate")
                    .Append(" FROM umbraconode mg")
                    .Append(" WHERE mg.nodeObjectType = '366e63b9-880f-4e13-a61c-98069b029728'");
                DataTable dt;

                if (userId.HasValue)
                {
                    var p = new[]
                    {
                        DBhelper.SetParameter("@userId", userId),
                    };
                    sbSql.Append(" AND mg.id IN (SELECT MemberGroup FROM CmsMember2MemberGroup mmg WHERE mmg.Member=@userId)");
                    dt = DBhelper.ExecuteDataset(sbSql.ToString(), p).Tables[0];
                }
                else
                {
                    dt = DBhelper.ExecuteDataset(sbSql.ToString()).Tables[0];
                }

                var list = new List<CmsMemberGroup>();
                foreach (DataRow dr in dt.Rows)
                {
                    var entity = new CmsMemberGroup
                    {
                        NodeID = dr["NodeId"] as int?,
                        MemberGroupName = dr["MemberGroupName"] as string,
                        CreateDate = dr["CreateDate"] as DateTime?
                    };
                    list.Add(entity);
                }

                return list;
            }
            catch (Exception ex)
            {
                //return ex.Message;
                throw ex;
            }
        }

        public static List<CmsMemberGroup> GetAllGroup()
        {
            try
            {
                var sbSql = new StringBuilder("SELECT mg.id NodeId, mg.text MemberGroupName, mg.CreateDate")
                    .Append(" FROM umbraconode mg")
                    .Append(" WHERE mg.nodeObjectType = '366e63b9-880f-4e13-a61c-98069b029728' ORDER BY MemberGroupName");
                DataTable dt;
                dt = DBhelper.ExecuteDataset(sbSql.ToString()).Tables[0];
                var list = new List<CmsMemberGroup>();
                foreach (DataRow dr in dt.Rows)
                {
                    var entity = new CmsMemberGroup
                    {
                        NodeID = dr["NodeId"] as int?,
                        MemberGroupName = dr["MemberGroupName"] as string,
                        CreateDate = dr["CreateDate"] as DateTime?
                    };
                    list.Add(entity);
                }

                return list;
            }
            catch (Exception ex)
            {
                //return ex.Message;
                throw ex;
            }
        }

        public static DataTable BindLogoRoles()
        {
            DataTable dt;
            string sql = "";
            string[] stringArray;
            string[] stringArr;
            try
            {
                string fs_sql = "select Company from crm_logo";
                dt = DBhelper.ExecuteDataset(fs_sql).Tables[0];

                if (dt.Rows.Count != 0)
                {
                    foreach (DataRow row in dt.Rows)
                    {
                        List<string> list = new List<string>();
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            string stringToCheck = dt.Rows[i]["Company"].ToString();
                            if (stringToCheck.Contains(","))
                            {
                                stringArr = stringToCheck.Split(',');
                                int j = stringArr.Length;
                                for (int k = 0; k < j; k++)
                                {
                                    list.Add(stringArr[k]);
                                }
                            }
                            else
                            {
                                list.Add(stringToCheck);
                            }
                        }
                        DataTable dtOutput = new DataTable();

                        sql = "SELECT mg.id NodeId,mg.text MemberGroupName,mg.CreateDate FROM umbraconode mg WHERE mg.nodeObjectType = '366e63b9-880f-4e13-a61c-98069b029728' and mg.text NOT IN (SELECT Company FROM crm_logo)";
                        dt = DBhelper.ExecuteDataset(sql).Tables[0];
                        stringArray = list.ToArray();
                        dtOutput = dt.DefaultView.ToTable();
                        foreach (string x in stringArray)
                        {
                            dtOutput.DefaultView.RowFilter = "[MemberGroupName] NOT LIKE '" + x + "%'";
                            dtOutput = dtOutput.DefaultView.ToTable();
                        }
                        return dtOutput;
                    }
                }
                else
                {
                    sql = "SELECT mg.id NodeId,mg.text MemberGroupName,mg.CreateDate FROM umbraconode mg WHERE mg.nodeObjectType = '366e63b9-880f-4e13-a61c-98069b029728' and mg.text NOT IN (SELECT crm_logo.Company FROM crm_logo)";
                    dt = DBhelper.ExecuteDataset(sql).Tables[0];
                }
            }
            catch (Exception)
            {
                throw;
            }
            return dt;
        }

        public static string getDefaultThemeforUser(int UserId)
        {
            string defaultCompany = string.Empty;
            string strquery = string.Empty;
            try
            {
                DataTable dtChk = DBhelper.ExecuteDataset(string.Format("Select userdefaultgroupid from crm_userdefaultcompany where UserId='{0}'", UserId)).Tables[0];
                if (dtChk.Rows.Count != 0)
                {
                    strquery = "SELECT IF(crm_theme.Theme IS NULL or crm_theme.Theme = '', 'default', crm_theme.Theme) as Theme FROM umbraconode AS mg INNER JOIN crm_theme ON mg.text = crm_theme.CompanyName WHERE mg.nodeObjectType = '366e63b9-880f-4e13-a61c-98069b029728' AND mg.id=" + dtChk.Rows[0]["userdefaultgroupid"];
                    defaultCompany = DBhelper.ExecuteScalar(strquery);
                }
                else
                    defaultCompany = "default";
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return defaultCompany;

        }
        public static DataTable BindAllCurrRole(int UserId)
        {
            DataTable dt;
            string sql = string.Empty;
            try
            {
                DataTable dtChk = DBhelper.ExecuteDataset(string.Format("Select userdefaultgroupid from crm_userdefaultcompany where UserId='{0}'", UserId)).Tables[0];

                if (dtChk.Rows.Count != 0)
                {
                    var values = dtChk.Rows[0]["userdefaultgroupid"];
                    sql = "SELECT mg.text AS MemberGroupName,mg.id, IF(crm_theme.Theme IS NULL or crm_theme.Theme = '', 'default', crm_theme.Theme) as Theme FROM umbraconode AS mg INNER JOIN crm_theme ON mg.text = crm_theme.CompanyName WHERE (mg.nodeObjectType = '366e63b9-880f-4e13-a61c-98069b029728') AND (mg.id IN(SELECT MemberGroup FROM CmsMember2MemberGroup AS mmg WHERE (mmg.Member = '" + UserId + "'))) Order By MemberGroupName Asc ";
                    dt = DBhelper.ExecuteDataset(sql).Tables[0];
                }
                else
                {
                    sql = "SELECT mg.text AS MemberGroupName, IF(crm_theme.Theme IS NULL or crm_theme.Theme = '', 'default', crm_theme.Theme) as Theme FROM umbraconode AS mg INNER JOIN crm_theme ON mg.text = crm_theme.CompanyName WHERE (mg.nodeObjectType = '366e63b9-880f-4e13-a61c-98069b029728') AND (mg.id IN(SELECT MemberGroup FROM CmsMember2MemberGroup AS mmg WHERE (mmg.Member = '" + UserId + "'))) Order By MemberGroupName Asc";
                    dt = DBhelper.ExecuteDataset(sql).Tables[0];
                    if (dt.Rows.Count == 0)
                    {
                        dt.Rows.Add("MF");
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }





    }
}
